#include "UBoat.h"
#include "Sonar.h"
#include<iostream>

#include"StringOps.h"
#include "Naview.h"
#include "Subview.h"
#include "CBaseView.h"

#include "Neptuner.h"
#include<fstream>
using namespace std;

UBoat::UBoat()
{
	m_pxRadar = Ark_getRadar();
	m_pxScubaPack = Ark_getScubaPack();
}

void UBoat::navigate(std::string sSourceDir, std::string sOutputDir, std::string sExtIncDir)
{
	scanDir(sSourceDir);
	buildInputFileList();
	parseInputFiles(sExtIncDir);
	createOutputFiles(sOutputDir);

	//m_pxRadar->dump();
}

void UBoat::buildInputFileList()
{

}

void UBoat::parseInputFiles(string sExtIncDir)
{
	size_t i=0, n= m_pxRadar->m_xCodebase.m_apxUnit.size();
	CodebaseUnit* pxItem;
	std::string sFilename;
	Scuba* pxScuba;

	for(i=0;i<n;i++)
	{
		pxItem = m_pxRadar->m_xCodebase.m_apxUnit[i];
		if(pxItem->m_eType != pxItem->eCBUFile)
			continue;

		sFilename = m_pxRadar->m_xCodebaseRoot.string();
		if(pxItem->m_sLocation!="") 
			sFilename += "/" + pxItem->m_sLocation;
		sFilename += "/" + pxItem->m_sName;

		cerr<<sFilename<<endl;
		if(Ark_isParsed(sFilename))
			continue;

		pxScuba = new Scuba;
		pxScuba->dive(sFilename);
	}
}

void UBoat::createOutputFiles(string sOutputDir)
{
	createCodebaseHomePage();
	createUBoatPages(sOutputDir);
	createSubmarinePages();
	deployAmmo();
}

void UBoat::scanDir(string sSourceDir)
{
	m_pxRadar->scan(sSourceDir,true, false);
}

void UBoat::deployAmmo()
{
	std::string sScriptDeployDir = m_pxRadar->m_xNeptunerRoot.string() +  "/uboat";
	Neptuner_copyDirectory(m_sScriptSourceDir, sScriptDeployDir);
}

void UBoat::createCodebaseHomePage()
{
	std::string sCodebaseHomePage = m_pxRadar->m_xNeptunerRoot.string()+ "/index.html";
	Neptuner_createDirectoryFor(sCodebaseHomePage);
	CBaseView xCBaseView;

	ofstream* pxOut = new std::ofstream(sCodebaseHomePage.c_str());
	xCBaseView.m_pxOut = pxOut;
	xCBaseView.m_pxRadar = m_pxRadar;
	xCBaseView.m_pxScubaPack = m_pxScubaPack;

	xCBaseView.generateHeader();
	xCBaseView.generateBody();
	xCBaseView.generateFooter();
}

void UBoat::createUBoatPages(string sOutputDir)
{
	size_t i=0, n=m_pxScubaPack->size();
	Scuba* pxScuba=NULL;
	for(i=0;i<n;i++)
	{
		pxScuba = m_pxScubaPack->at(i);
		Naview xNaview;

		xNaview.m_nLines = pxScuba->getNumberOfLines();
		xNaview.m_sFilename = m_pxRadar->getCodebaseLocation(pxScuba->m_sFilename);
		xNaview.m_nLevel = Neptuner_getLevel(xNaview.m_sFilename);
		
		string sOutputFile = m_pxRadar->m_xNeptunerRoot.string() + "/codebase/" + xNaview.m_sFilename;	
		xNaview.setOutputFile(sOutputFile);

		xNaview.generateHeader();
		xNaview.generateBody();
		pxScuba->walk(xNaview);
		xNaview.generateFooter();
	}
}

void UBoat::createSubmarinePages()
{
	size_t i=0, n=m_pxScubaPack->size();
	Scuba* pxScuba=NULL;
	for(i=0;i<n;i++)
	{
		pxScuba = m_pxScubaPack->at(i);

		Subview xSubview;

		xSubview.m_pxFuncalyzer = pxScuba->m_pxFuncalyzer;
		xSubview.m_nLines = pxScuba->getNumberOfLines();
		xSubview.m_sFilename = m_pxRadar->getCodebaseLocation(pxScuba->m_sFilename);
		xSubview.m_nLevel = Neptuner_getLevel(xSubview.m_sFilename);
		
		string sOutputFile = m_pxRadar->m_xNeptunerRoot.string() + "/submarine/" + xSubview.m_sFilename;	
		xSubview.setOutputFile(sOutputFile);

		xSubview.generateHeader();
		xSubview.generateBody();
		xSubview.generateFooter();
	}
}
